﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.Catalog
{
    public class CatalogItemBL
    {
        public void clsCataLogItemInsert(tbl_CatalogItem catelogitems)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_CatalogItem catalogitem = new tbl_CatalogItem();
                    catalogitem.CatalogItemsName = catelogitems.CatalogItemsName;
                    catalogitem.ImagesPath = catelogitems.ImagesPath;
                    catalogitem.CatalogLink = catelogitems.CatalogLink;
                    catalogitem.Description = catelogitems.Description;
                    catalogitem.IndexShow = catelogitems.IndexShow;
                    catalogitem.CatalogNo = catelogitems.CatalogNo;
                    catalogitem.CatalogName = catelogitems.CatalogName;
                    catalogitem.CreatedUser = catelogitems.CreatedUser;
                    catalogitem.CreatedDate = DateTime.Now;
                    catalogitem.Status = catelogitems.Status;

                    adlerContextDb.tbl_CatalogItems.InsertOnSubmit(catalogitem);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        public void clsCataLogItemModify(tbl_CatalogItem catelogitems)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var catalogitem = adlerContextDb.tbl_CatalogItems.Single(d => d.CatalogItemsNo == catelogitems.CatalogItemsNo);
                    catalogitem.CatalogItemsName = catelogitems.CatalogItemsName;
                    catalogitem.ImagesPath = catelogitems.ImagesPath;
                    catalogitem.CatalogLink = catelogitems.CatalogLink;
                    catalogitem.Description = catelogitems.Description;
                    catalogitem.IndexShow = catelogitems.IndexShow;
                    catalogitem.CatalogNo = catelogitems.CatalogNo;
                    catalogitem.CatalogName = catelogitems.CatalogName;
                    catalogitem.Status = catelogitems.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        public void clsCataLogItemDelete(tbl_CatalogItem catelogitems)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var catalogitem = adlerContextDb.tbl_CatalogItems.Single(d => d.CatalogItemsNo == catelogitems.CatalogItemsNo);
                    catalogitem.Status = catelogitems.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        // Show CatalogItem.
        public List<MODEL_SP_ADMIN_SEARCH_CATALOGITEM> Get_Admin_Catalogitem(string catalogno, string catalogname, string status)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                int index = 1;
                //var query = from n in contextdb.SP_ADMIN_SEARCH_CATALOGITEM(catalogno, catalogname, status)
                //            select new MODEL_SP_ADMIN_SEARCH_CATALOGITEM
                //            {
                //                Index = index++,
                //                CatalogItemsNo = n.CatalogItemsNo,
                //                CatalogItemsName = n.CatalogItemsName,
                //                ImagesPath = n.ImagesPath,
                //                CatalogLink = n.CatalogLink,
                //                Description = n.Description,
                //                IndexShow = n.IndexShow,
                //                CatalogNo = n.CatalogNo,
                //                CatalogName = n.CatalogName,
                //                CreatedUser = n.CreatedUser,
                //                CreatedDate = n.CreatedDate,
                //                Status = n.Status
                //            };
                //return query.ToList();
                return null;
            }
        }

        public tbl_CatalogItem GetCatalogitemByNo(int catalogitemno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                return contextdb.tbl_CatalogItems.Single(d => d.CatalogItemsNo == catalogitemno);
            }
        }
    }
}
